<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['cpcode'])){
		$params['cpcode like']='%'.trim($_RQ['cpcode']).'%';
		$return['params']['cpcode']=$_RQ['cpcode'];
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_fahuo_tpl',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'打印面单模板'),
		),
		'list'=>array('data'=>'list','edit'=>false,'total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'cpcode','title'=>'快递编号','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'string'),
			array('name'=>'url','title'=>'模板地址','type'=>'string_copy'),
			array('name'=>'title','title'=>'模板名称','type'=>'string'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'batch'=>array(
			array('type'=>'post','url'=>'/tpls/update/','title'=>'更新普通模板'),
			array('type'=>'post','url'=>'/tpls/updateisv/','title'=>'更新ISV下自定义模板'),
		)),
		'search'=>array(
			array('name'=>'cpcode','width'=>'15','title'=>'快递编号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='update' || $_RQ['op']=='updateisv'){
	if($_RQ['op']=='update'){
		$templateUrldata=$this->model('api')->get_template($cpCode);
	}else{
		$templateUrldata=$this->model('api')->get_template($cpCode,1);
	}
	if(count($templateUrldata)>0){
		foreach($templateUrldata as $item){
			foreach($item['standardTemplateDOs'] as $iv){
				$id=pdo_getcolumn('jy_fahuo_tpl',array('cpcode'=>$item['cpCode'],'type'=>$iv['standardWaybillType'],'uniacid'=>$_SESSION['uniacid']),'id');
				$data=array(
					'cpcode'=>$item['cpCode'],
					'type'=>$iv['standardWaybillType'],
					'url'=>$iv['standardTemplateUrl'],
					'title'=>$iv['standardTemplateName'],
				);
				if($id){
					pdo_update('jy_fahuo_tpl',$data,array('id'=>$id));
				}else{
					$data['createtime']=TIMESTAMP;
					$data['uniacid']=$_SESSION['uniacid'];
					pdo_insert('jy_fahuo_tpl',$data);
				}
			}
		}
		exi('更新成功！');
	}else{
		exi('更新失败！没获取到任何模板！','error');
	}
}
?>